//获取应用实例
var config = require('../../config/config.js');

Page({
  data: {
    xcx_name: config.xcx_name
  },
  onLoad: function (options) {
    console.log(options)
    var params = options.type;
    if (params != '' && params != undefined && params == 'clear') {
      wx.setStorageSync('member_info', '');
    }
    var flag = true;
    var member_info = wx.getStorageSync('member_info');
    if (member_info == '') {
      flag = false;
    }
    if (member_info == undefined) {
      flag = false;
    }
    if (!member_info) {
      flag = false;
    }
    if (typeof (member_info) == 'object') {
      flag = false;
      for (var key in member_info) {
        flag = true;
        break;
      }
    }
    if (flag) {
      this.setData({
        xcx_openid: member_info.xcx_openid,
        sid: member_info.sid
      })
      wx.navigateTo({
        url: '../qds/buyWater/index',
      })
      return
    }
  },

  goQds: function () {
    wx.navigateTo({
      url: '../qds/index/index', //
    })
  },
  goTest: function () {
    wx.navigateTo({
      url: '../qds/showResult/pay?ispaysuccess=1&id=161', //
    })
  },
  clear: function () {
    wx.setStorageSync('member_info', '');
  },

  saveFormId: function (e) {
    var formId = e.detail.formId;
    console.log(formId);
    var member_info = wx.getStorageSync('member_info');
    var xcx_openid = member_info != undefined ? member_info.xcx_openid : '';
    var sid = member_info != undefined ? member_info.sid : '';
    wx.login({
      success: function (res) {
        if (res.code) {
          wx.getUserInfo({
            success: function (result) {
              console.log('ok');
              console.log(result);
              wx.request({
                //后台接口地址
                url: config.wxlogin,
                data: {
                  code: res.code,
                  encryptedData: result.encryptedData,
                  iv: result.iv,
                  aid: config.aid,
                  xcx_openid: xcx_openid,
                  sid: sid,
                  formId: formId
                },
                method: 'GET',
                header: {
                  'content-type': 'application/json'
                },
                success: function (res) {
                  if (!res.data.status) {
                    wx.showModal({
                      title: '提示',
                      content: res.data.msg,
                    })
                    return
                  }
                  wx.setStorageSync('member_info', res.data.data);
                  if (res.data.data.token != undefined) {
                    wx.setStorageSync('token', res.data.data.token);
                    wx.navigateTo({
                      url: '../qds/shopChoose/index'
                    })
                  } else {
                    wx.navigateTo({
                      url: '../qds/buyWater/index',
                    })
                  }
                },
                fail: function (res) {
                  wx.showModal({
                    title: '提示',
                    content: '未知错误,请稍后再试',
                  })
                }
              })
            },
            fail: function () {
              console.log('no');
              wx.showModal({
                title: '提示',
                content: '微信官方授权失败,请稍后再试',
              })
            }
          })
        } else {
          wx.showModal({
            title: '提示',
            content: '微信官方授权失败,请稍后再试',
          })
        }
      },
      fail: function (res) {
        wx.showModal({
          title: '提示',
          content: '微信官方授权失败,请稍后再试',
        })
      }
    })
  },
})